﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbNetLink.Data;
using System.Xml.XPath;
using System.IO;
using System.Xml;

namespace Projeto.TCC.Dados
{
    class BancoDados
    {
        private string SGBD = "";
        private string StrConexao = "";

        public BancoDados()
        {
            carregaConfiguracoesBD();
        }

        public string getSGBD()
        {
            switch (SGBD)
            {
                case "ODBC":
                    return "ODBC";
                case "SYBASE":
                    return "Sybase Anywhere";
                case "POSTGRESQL":
                    return "PostgreSQL";
                case "MYSQL":
                    return "MySQL";
                case "DB2":
                    return "DB2";
                case "FIREBIRD":
                    return "Firebird";
                case "ORACLE":
                    return "Oracle";
                case "SQLSERVER":
                    return "SQLServer";
                case "SQLITE":
                    return "SQLite";
            }
            return "ODBC";
        }

        public string getConnStr()
        {
            return StrConexao;
        }

        public void setConnStr(string Valor)
        {
            StrConexao = Valor;
        }

        public void setSGBD(string Valor)
        {
            switch (SGBD)
            {
                case "ODBC":
                    SGBD = "ODBC";
                    break;
                case "SYBASE":
                case "Sybase Anywhere":
                    SGBD = "SYBASE";
                    break;
                case "POSTGRESQL":
                case "PostgreSQL":
                    SGBD = "POSTGRESQL";
                    break;
                case "MYSQL":
                case "MySQL":
                    SGBD = "MYSQL";
                    break;
                case "DB2":
                    SGBD = "DB2";
                    break;
                case "FIREBIRD":
                case "Firebird":
                    SGBD = "FIREBIRD";
                    break;
                case "ORACLE":
                case "Oracle":
                    SGBD = "ORACLE";
                    break;
                case "SQLSERVER":
                case "SQLServer":
                    SGBD = "SQLSERVER";
                    break;
                case "SQLITE":
                case "SQLite":
                    SGBD = "SQLITE";
                    break;
            }
        }

        public void atualizaConfigBD()
        {
            carregaConfiguracoesBD();
        }
        
        private void carregaConfiguracoesBD()
        {
            if (File.Exists("initConfig.XML"))
            {
                XPathDocument xpathDoc = new XPathDocument("initConfig.XML");
                XPathNavigator xpathNav = xpathDoc.CreateNavigator();
                StrConexao = xpathNav.SelectSingleNode("/database/ConnStr").Value;
                SGBD = xpathNav.SelectSingleNode("/database/SGBD").Value;
                
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load("D:/kati/Sistemas de Informação/TCC/SkyDrive/Desenvolvimento/BDSetrem/BDSetrem/bin/Debug/initConfig.xml");
                XmlNodeList nodeList = xDoc.GetElementsByTagName("connection");
                string server = nodeList[0].ChildNodes[0].InnerText;
                string porta = nodeList[0].ChildNodes[1].InnerText;
                string user = nodeList[0].ChildNodes[2].InnerText;
                string pass = nodeList[0].ChildNodes[3].InnerText;
                string database = nodeList[0].ChildNodes[4].InnerText;


                string conexao = "Server=" + server + ";Port=" + porta + ";User Id=" + user + ";Password=" + pass + ";Database=" + database + "";
            }
            else
            {
                XmlDocument xmlConfig = new XmlDocument();
                XmlDeclaration dec = xmlConfig.CreateXmlDeclaration("1.0", null, null);
                xmlConfig.AppendChild(dec);
                XmlElement root = xmlConfig.CreateElement("database");

                XmlElement Sgbd = xmlConfig.CreateElement("SGBD");
                Sgbd.InnerText = "ODBC";

                root.AppendChild(Sgbd);

                XmlElement StringConexao = xmlConfig.CreateElement("ConnStr");
                StringConexao.InnerText = "DSN=GESPAM;";

                root.AppendChild(StringConexao);

                xmlConfig.AppendChild(root);

                xmlConfig.Save("PortalTransparencia.XML");
            }
        }

        public void salvarConfiguracoesBD()
        {
            if (File.Exists("initConfig.XML"))
            {
                File.Delete("initConfig.XML");
            }

            XmlDocument xmlConfig = new XmlDocument();
            XmlDeclaration dec = xmlConfig.CreateXmlDeclaration("1.0", null, null);
            xmlConfig.AppendChild(dec);
            XmlElement root = xmlConfig.CreateElement("database");

            XmlElement Sgbd = xmlConfig.CreateElement("SGBD");
            Sgbd.InnerText = SGBD;

            root.AppendChild(Sgbd);

            XmlElement StringConexao = xmlConfig.CreateElement("ConnStr");
            StringConexao.InnerText = StrConexao;

            root.AppendChild(StringConexao);

            xmlConfig.AppendChild(root);

            xmlConfig.Save("initConfig.XML");

        }

        public DbNetData retornaBD()
        {
            switch (SGBD)
            {
                case "ODBC":
                    return bdODBC();
                case "SYBASE":
                    return bdSYBASE();
                case "POSTGRESQL":
                    return bdPostgreSQL();
                case "MYSQL":
                    return bdMySQL();
                case "DB2":
                    return bdDB2();
                case "FIREBIRD":
                    return bdFirebird();
                case "ORACLE":
                    return bdOracle();
                case "SQLSERVER":
                    return bdSQLServer();
                case "SQLITE":
                    return bdSQLite();
            }

            return bdODBC();
        }

        private DbNetData bdODBC()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.Odbc, DatabaseType.Unknown);
            return Bd;
        }

        private DbNetData bdSYBASE()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.Odbc, DatabaseType.Sybase);
            return Bd;
        }

        private DbNetData bdPostgreSQL()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.Npgsql, DatabaseType.PostgreSql);
            return Bd;
        }

        private DbNetData bdMySQL() // se bd for Mysql
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.MySql, DatabaseType.MySql);
            return Bd;
        }

        private DbNetData bdDB2()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.DB2, DatabaseType.DB2);
            return Bd;
        }

        private DbNetData bdFirebird()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.Firebird, DatabaseType.Firebird);
            return Bd;
        }

        private DbNetData bdOracle()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.Oracle, DatabaseType.Oracle);
            return Bd;
        }

        private DbNetData bdSQLServer()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.SqlClient, DatabaseType.SqlServer);
            return Bd;
        }

        private DbNetData bdSQLite()
        {
            DbNetData Bd = new DbNetData(StrConexao, DataProvider.SQLite, DatabaseType.SQLite);
            return Bd;
        }
    }
}